#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 500005

int t,n;
int ex[maxn],ey[maxn],nx[maxn],ny[maxn];
int discret[2*maxn];

class UnionSet {
private:
    int father[2*maxn];
public:
    int find(int a){
        if(father[a]!=a) return father[a]=find(father[a]);
        else return father[a];
    }
    void unite(int a, int b){
        int fa=find(a), fb=find(b);
        if(fa!=fb) father[fa]=fb;
    }
    void clear(){
        for(int i=0;i<2*maxn;i++)
            father[i]=i;
    }
    UnionSet(){
        clear();
    }
}us;

int main(){
    cin>>t;
    for(int tt=0;tt<t;tt++){
        int enu=0, nun=0, aun=0;
        us.clear();
        cin>>n;
        for(int i=0;i<n;i++){
            int x,y,e;
            cin>>x>>y>>e;
            discret[aun++]=x; discret[aun++]=y;
            if(e==1){ ex[enu]=x; ey[enu]=y; enu++; }
            else if(e==0){ nx[nun]=x; ny[nun]=y; nun++; }
        }
        sort(discret, discret+aun);
        int disnum = unique(discret, discret+aun)-discret;
        for(int i=0; i<enu; i++){
			ex[i] = lower_bound(discret, discret+disnum, ex[i])-discret;
			ey[i] = lower_bound(discret, discret+disnum, ey[i])-discret;
		}
		for(int i=0; i<nun; i++){
			nx[i] = lower_bound(discret, discret+disnum, nx[i])-discret;
			ny[i] = lower_bound(discret, discret+disnum, ny[i])-discret;
		}
        for(int i=0;i<enu;i++){
            us.unite(ex[i],ey[i]);
        }
        for(int i=0;i<nun;i++){
            if(us.find(nx[i]) == us.find(ny[i])){
                cout<<"NO"<<endl;
                goto end;
            }
        }
        cout<<"YES"<<endl;
end:
        ;
    }
    return 0;
}